package com.logic.landseaserver.ws.interceptor;

import com.logic.common.enums.LogTypeEnum;
import com.logic.common.security.SecurityUtils;
import com.logic.common.util.SysUtil;
import com.logic.landseaserver.common.enums.LogModuleTypeEnum;
import com.logic.landseaserver.domain.Project;
import com.logic.landseaserver.service.IOperationLogService;
import com.logic.landseaserver.service.impl.ProjectServiceImpl;
import com.logic.landseaserver.ws.ContractRest;
import com.logic.landseaserver.ws.dto.ProjectReqDTO;
import com.logic.system.service.impl.LogService;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;

/**
 * Created by quintina on 2017/8/10.
 */
@Aspect
@Component
public class ProjectLogAspect {
    private static final Logger logger = LoggerFactory.getLogger(ContractRest.class);


    @Autowired
    private IOperationLogService operationLogService;

    @Pointcut("execution(* com.logic.landseaserver.service.impl.ProjectServiceImpl.addOrEditProject*(..))")
    public void createPointCut() {
    }

    @Pointcut("execution(* com.logic.landseaserver.service.impl.ProjectServiceImpl.update*(..))")
    public void updatePointCut() {
    }


    @Pointcut("execution(* com.logic.landseaserver.service.impl.ProjectServiceImpl.delete*(..))")
    public void deletePointCut() {
    }

    @AfterReturning(returning = "rvt", pointcut = "execution(* com.logic.landseaserver.service.impl.ProjectServiceImpl.insert*(..))")
    public void AfterReturning(Object rvt)  {
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();

        operationLogService.log(SysUtil.getClientIpAddr(request),
                    SecurityUtils.getCurrentLogin().getUserId(),  "上传项目",
                    LogTypeEnum.EDIT.name(), LogModuleTypeEnum.PROJECT.getCode(), (Integer) rvt);

    }

    @After(value = "updatePointCut()")
    public void updateAfterCall(JoinPoint joinPoint) throws ClassNotFoundException {

        Object[] arguments = joinPoint.getArgs();
        Project projectReqDTO = (Project) arguments[0];
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();

        if (projectReqDTO.getId() != null && ("LPAP1").equals(projectReqDTO.getPublicStatus())) {
            operationLogService.log(SysUtil.getClientIpAddr(request),
                    SecurityUtils.getCurrentLogin().getUserId(),  "官网发布",
                    LogTypeEnum.EDIT.name(), LogModuleTypeEnum.PROJECT.getCode(),projectReqDTO.getId());
        }
        if (projectReqDTO.getId() != null && ("LPB2").equals(projectReqDTO.getManageStatus()) && ("LPA2").equals(projectReqDTO.getStatus())) {
            operationLogService.log(SysUtil.getClientIpAddr(request),
                    SecurityUtils.getCurrentLogin().getUserId(),  "发布项目",
                    LogTypeEnum.EDIT.name(), LogModuleTypeEnum.PROJECT.getCode(),projectReqDTO.getId());
        }
        if (projectReqDTO.getId() != null && ("LPB1").equals(projectReqDTO.getManageStatus()) && ("LPA4").equals(projectReqDTO.getStatus()) && ("LPAP2").equals(projectReqDTO.getPublicStatus())) {
            operationLogService.log(SysUtil.getClientIpAddr(request),
                    SecurityUtils.getCurrentLogin().getUserId(),  "取消发布",
                    LogTypeEnum.EDIT.name(), LogModuleTypeEnum.PROJECT.getCode(),projectReqDTO.getId());
        }
        if (projectReqDTO.getId() != null && ("LPAP2").equals(projectReqDTO.getPublicStatus())) {
            operationLogService.log(SysUtil.getClientIpAddr(request),
                    SecurityUtils.getCurrentLogin().getUserId(),  "取消官网发布",
                    LogTypeEnum.EDIT.name(), LogModuleTypeEnum.PROJECT.getCode(),projectReqDTO.getId());
        }
        if (projectReqDTO.getId() != null && ("LPAP2").equals(projectReqDTO.getPublicStatus()) && ("LPA3").equals(projectReqDTO.getStatus())) {
            operationLogService.log(SysUtil.getClientIpAddr(request),
                    SecurityUtils.getCurrentLogin().getUserId(),  "关闭项目",
                    LogTypeEnum.EDIT.name(), LogModuleTypeEnum.PROJECT.getCode(),projectReqDTO.getId());
        }
    }

    @After(value = "deletePointCut()")
    public void deleteAfterCall(JoinPoint joinPoint) throws ClassNotFoundException {

        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();

        operationLogService.log(SysUtil.getClientIpAddr(request),
                SecurityUtils.getCurrentLogin().getUserId(),  "关闭项目",
                LogTypeEnum.DELETE.name(), LogModuleTypeEnum.PROJECT.getCode(),null);

    }

}
